import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/patientInfo',
    },
    // {
    //     path: '/dashboard',
    //     name: 'dashboard',
    //     meta: {
    //         title: '系统首页',
    //         noAuth: true,
    //     },
    //     component: () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard.vue'),
    // },
    // {
    //     path: '/home',
    //     name: 'home',
    //     meta: {
    //         title: '系统首页',
    //         noAuth: true,
    //     },
    //     component: () => import(/* webpackChunkName: "dashboard" */ '../views/mobile/home/index.vue'),
    // },
    {
        path: '/patientInfo',
        name: 'patientInfo',
        meta: {
            title: '系统首页',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "dashboard" */ '../views/mobile/insurance/patient-info.vue'),
    },
    {
        path: '/insuranceCompany',
        name: 'insuranceCompany',
        meta: {
            title: '社保公司',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "dashboard" */ '../views/mobile/insurance/insurance-compony.vue'),
    },
    {
        path: '/insuraneInfo',
        name: 'insuraneInfo',
        meta: {
            title: '社保公司',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "dashboard" */ '../views/mobile/insurance/insurane-info.vue'),
    },
    {
        path: '/shapping',
        name: 'shapping',
        meta: {
            title: '社保公司',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "dashboard" */ '../views/mobile/insurance/shapping.vue'),
    },
    {
        path: '/finish',
        name: 'finish',
        meta: {
            title: '社保公司',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "dashboard" */ '../views/mobile/insurance/finish.vue'),
    },

    // {
    //     path: '/login',
    //     meta: {
    //         title: '登录',
    //         noAuth: true,
    //     },
    //     component: () => import(/* webpackChunkName: "login" */ '../views/pages/login.vue'),
    // },
    // {
    //     path: '/403',
    //     meta: {
    //         title: '没有权限',
    //         noAuth: true,
    //     },
    //     component: () => import(/* webpackChunkName: "403" */ '../views/pages/403.vue'),
    // },
    // {
    //     path: '/404',
    //   redirect: '/patientInfo',
    // },
    // { path: '/:path(.*)', redirect: '/404' },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    const role = localStorage.getItem('vuems_name');
    const permiss = usePermissStore();

    if (!role && to.meta.noAuth !== true) {
        next('/login');
    } else if (typeof to.meta.permiss == 'string' && !permiss.key.includes(to.meta.permiss)) {
        // 如果没有权限，则进入403
        next('/403');
    } else {
        next();
    }
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
